package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.CoinPrice;
import com.juyu.miliao.api.service.cache.CoinPriceCacheService;
import com.juyu.miliao.common.constant.AppPackageIdConstant;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.ConfigApp;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CoinPriceService {
    @Resource
    CoinPriceCacheService coinPriceCacheService;
    @Resource
    private ConfigAppService configAppService;
    @Resource
    private UserRedisDao userRedisDao;
    private static final Map<Integer,Integer> coinTypeAndConfigIdMap = new HashedMap();

    /**
     * map<coinType,configAppId>
     */
    CoinPriceService(){
        coinTypeAndConfigIdMap.put(1,1);
        coinTypeAndConfigIdMap.put(2,3);
        coinTypeAndConfigIdMap.put(3,2);
        coinTypeAndConfigIdMap.put(5,9);
        coinTypeAndConfigIdMap.put(6,5);
        coinTypeAndConfigIdMap.put(7,6);
        coinTypeAndConfigIdMap.put(8,2);
        coinTypeAndConfigIdMap.put(9,12);
        coinTypeAndConfigIdMap.put(10,13);
        coinTypeAndConfigIdMap.put(11,14);
        coinTypeAndConfigIdMap.put(12,1);
        coinTypeAndConfigIdMap.put(13,26);
    }

    public List<CoinPrice> getCoinPriceList(CommonHead com,long userId) {
        ConfigApp configApp = configAppService.getConfigApp(com.getAppPackageId(),com.getPhoneSystem());
        List<CoinPrice> coinPriceList = findCoinPriceListByConfigAppId(configApp.getId(),0,0);
        if(coinPriceList==null || coinPriceList.size()==0){
            coinPriceList = findCoinPriceListByConfigAppId(1,0,0);
        }
        //1.5.5首冲
        if(userRedisDao.isMemberFirstRechargeUser(userId)){
            coinPriceList = coinPriceList.stream().filter(i->i.getCoinNumber()!=800).collect(Collectors.toList());
        }
        return coinPriceList;
    }

    /**
     * 根据configAppId返回列表
     * @param configAppId
     * @param featureType 支付功能类型 0: app支付,1: MWEB--H5支付，2：JSAPI--JSAPI(公众号号)，
     *                    3：JSAPI--JSAPI(小程序)，4：NATIVE--Native支付
     * @return
     */
    private List<CoinPrice> findCoinPriceListByConfigAppId(long configAppId,int featureType,int isWindow) {
        List<CoinPrice> coinPriceList = coinPriceCacheService.findCoinPriceList();
        List<CoinPrice> resultList = new ArrayList<>();
        coinPriceList.forEach(coinPrice -> {
            if (coinPrice.getConfigAppId() == configAppId
                    && coinPrice.getPayFeatureType()==featureType
                    && coinPrice.getIsWindow() == isWindow)
                resultList.add(coinPrice);
        });
        return resultList;
    }

    public List<CoinPrice> getPayCoinList(CommonHead com) {
        if (AppPackageIdConstant.TONG_CHENG_MI_LIAO.equals(com.getAppPackageId())) {
            return getCoinPriceListTimeOutCacheByType(EnumType.COIN_TYPE_TCYL.state).subList(0, 3);
        } else if (AppPackageIdConstant.MO_MO_YUE_HUI.equals(com.getAppPackageId())) {
            return getCoinPriceListTimeOutCacheByType(EnumType.COIN_TYPE_MMJY.state);
        }
        throw new ConsumeRuntimeException("此包不允许调用");
    }

    public List<CoinPrice> getCoinPriceListByCoinType(int type) {
        int configAppId = coinTypeAndConfigIdMap.get(type);
        int featureType = 0;
        if(type==8){
            featureType = 1;
        }else if(type==12){
            featureType = 2;
        }
        return findCoinPriceListByConfigAppId(configAppId,featureType,0);
    }

    private List<CoinPrice> getCoinPriceListTimeOutCacheByType(int type) {
        List<CoinPrice> coinPriceList = coinPriceCacheService.findCoinPriceList();
        List<CoinPrice> resultList = new ArrayList<>();
        coinPriceList.forEach(coinPrice -> {
            if (coinPrice.getCoinType() == type)
                resultList.add(coinPrice);
        });
        return resultList;
    }

    public CoinPrice getCoinPriceById(int id) {
        List<CoinPrice> coinPriceList = coinPriceCacheService.findCoinPriceList();
        if (coinPriceList.size()==0)
            return null;
        Map<Integer, CoinPrice> coinPriceMap = coinPriceList.stream()
                .collect(Collectors.toMap(CoinPrice::getCoinId, Function.identity()));
        return coinPriceMap.get(id);
    }


    public List<CoinPrice> getCoinPriceWindowList(CommonHead com) {
        ConfigApp configApp = configAppService.getConfigApp(com.getAppPackageId(),com.getPhoneSystem());
        List<CoinPrice> coinPriceList = findCoinPriceListByConfigAppId(configApp.getId(),0,1);
        if(coinPriceList==null || coinPriceList.size()==0){
            coinPriceList = findCoinPriceListByConfigAppId(1,0,1);
        }
        return coinPriceList;
    }
}
